﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Scheduling.Interfaces.ViewModels;
using Presentation.Module.Scheduling.Interfaces.Services;
using Microsoft.Practices.Prism.Regions;
using Presentation.Module.Scheduling.Interfaces.Views;
using Presentation.Module.Scheduling.ViewDto;
using Microsoft.Practices.Prism.Commands;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;

using Microsoft.Practices.Prism.Regions;

namespace Presentation.Module.Scheduling.ViewModels
{
    
    public class ScheduleStudyViewModel : IScheduleStudyViewModel, INotifyPropertyChanged
    {
        private List<Presentation.Module.Scheduling.ViewDto.ScheduleStudy> _ScheduleStudys;
        
        // Constructor injection, injecting a scheduleStudy service instance
        public ScheduleStudyViewModel(IScheduleStudyService scheduleStudyService, IAddScheduleStudyView addScheduleStudyView, IRegionManager regionManager) // IEditSkillView editScheduleStudyView,
        {
            this.ScheduleStudyService = scheduleStudyService;
            this.RegionManager = regionManager;
            
            // Get all the scheduleStudys and store in a property
            this.ScheduleStudys = scheduleStudyService.GetAllScheduleStudys();

            //Views
            //EditSkillView = editSkillView;
            AddScheduleStudyView = addScheduleStudyView;

            //this.RegionManager.Regions["MainRegion"].Add(EditScheduleStudyView);
            this.RegionManager.Regions["MainRegion"].Add(AddScheduleStudyView);
            
           // EditScheduleStudyView.ViewExit += AddEditScheduleStudyView_ViewExit;
            AddScheduleStudyView.ViewExit += AddEditScheduleStudyView_ViewExit;


           

            // Wire up commands to implementations
            this.EditScheduleStudy = new DelegateCommand<ScheduleStudy>(OnEditScheduleStudy);
            this.AddScheduleStudy = new DelegateCommand<ScheduleStudy>(OnAddScheduleStudy);
            this.RemoveScheduleStudy = new DelegateCommand<ScheduleStudy>(OnRemoveScheduleStudy);
        }

        void AddEditScheduleStudyView_ViewExit()
        {
            this.RegionManager.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("ScheduleStudys");
        }

        // List of ScheduleStudys - View binds to these
        public List<Presentation.Module.Scheduling.ViewDto.ScheduleStudy> ScheduleStudys
        {
            get
            {
                return this.ScheduleStudyService.GetAllScheduleStudys();
            }
            set
            {
                if (value != null)
                {
                    _ScheduleStudys = value;
                    RaiseOnPropertyChanged("ScheduleStudys");
                }
            }
        }

        public IScheduleStudyView View { get; set; }
        //public IEditSkillView EditSkillView { get; set; }
        public IAddScheduleStudyView AddScheduleStudyView { get; set; }
        public IRegionManager RegionManager { get; set; }

        #region Commands - View binds to these for behaviour
        public ICommand EditScheduleStudy { get; private set; }
        public ICommand AddScheduleStudy { get; private set; }
        public ICommand RemoveScheduleStudy { get; private set; }
        #endregion

        private IScheduleStudyService ScheduleStudyService { get; set; }

        #region Command implementations
        private void OnEditScheduleStudy(ScheduleStudy scheduleStudy)
        {
            //EditScheduleStudyView.EditScheduleStudy(scheduleStudy);

            //this.RegionManager.Regions["MainRegion"].Activate(EditScheduleStudyView);


            //this.ScheduleStudys = ScheduleStudyService.GetAllStudySchedules();
        }

        private void OnAddScheduleStudy(ScheduleStudy scheduleStudy)
        {
            AddScheduleStudyView.AddScheduleStudy();

            this.RegionManager.Regions["MainRegion"].Activate(AddScheduleStudyView);


            this.ScheduleStudys = ScheduleStudyService.GetAllScheduleStudys();
            this.RaiseOnPropertyChanged("ScheduleStudys");
        }

        private void OnRemoveScheduleStudy(ScheduleStudy scheduleStudy)
        {
            if (MessageBox.Show("Are you sure that you want to delete this item?", "Delete Study", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                ScheduleStudyService.RemoveScheduleStudy(scheduleStudy);
            }
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        

    }



}
